package com.sychip.fhc.app.drawer.nav


import android.app.Activity
import androidx.activity.compose.BackHandler
import androidx.compose.animation.EnterTransition
import androidx.compose.animation.ExitTransition
import androidx.compose.foundation.Image
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.ExperimentalLayoutApi
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.WindowInsets
import androidx.compose.foundation.layout.consumeWindowInsets
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.navigationBars
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.requiredWidth
import androidx.compose.foundation.layout.width
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.automirrored.filled.List
import androidx.compose.material.icons.filled.Home
import androidx.compose.material.icons.filled.KeyboardCommandKey
import androidx.compose.material.icons.filled.Person
import androidx.compose.material.icons.filled.Settings
import androidx.compose.material3.DrawerState
import androidx.compose.material3.DrawerValue
import androidx.compose.material3.Icon
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.MaterialTheme.colorScheme
import androidx.compose.material3.ModalDrawerSheet
import androidx.compose.material3.ModalNavigationDrawer
import androidx.compose.material3.Scaffold
import androidx.compose.material3.Surface
import androidx.compose.material3.Text
import androidx.compose.material3.TextButton
import androidx.compose.material3.rememberDrawerState
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.MutableFloatState
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableFloatStateOf
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.alpha
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.RectangleShape
import androidx.compose.ui.graphics.painter.Painter
import androidx.compose.ui.graphics.vector.ImageVector
import androidx.compose.ui.res.dimensionResource
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.unit.dp
import androidx.hilt.navigation.compose.hiltViewModel
import androidx.lifecycle.compose.collectAsStateWithLifecycle
import androidx.navigation.NavGraph.Companion.findStartDestination
import androidx.navigation.NavGraphBuilder
import androidx.navigation.NavHostController
import androidx.navigation.NavType
import androidx.navigation.compose.NavHost
import androidx.navigation.compose.composable
import androidx.navigation.compose.rememberNavController
import androidx.navigation.navArgument
import androidx.navigation.navOptions
import com.fhc.connectivity.ConnectionState
import com.fhc.connectivity.NetworkViewModel
import com.fhc.view.GlobalLoadingView
import com.fhc.view.utils.ToastyShow
import com.infinum.snacky.SnackyAnimationSpec
import com.infinum.snacky.SnackyHost
import com.infinum.snacky.default.ShowQuestionSnackbar
import com.infinum.snacky.rememberSnackyHostState
import com.sychip.fhc.app.data.source.dto.TaskDto
import com.sychip.fhc.app.drawer.screen.CanvasScreen
import com.sychip.fhc.app.drawer.screen.DialogSampleScreen
import com.sychip.fhc.app.drawer.screen.SettingsScreen
import com.sychip.fhc.app.drawer.screen.addedittask.AddEditTaskScreen
import com.sychip.fhc.app.drawer.screen.components.ComponentsScreen
import com.sychip.fhc.app.drawer.screen.statistics.StatisticsScreen
import com.sychip.fhc.app.drawer.screen.tasks.TasksScreen
import com.sychip.fhc.app.todo.R
import com.sychip.fhc.app.todo.TodoDestinationsArgs
import com.sychip.fhc.AppCoverScreen
import com.sychip.fhc.app.drawer.screen.BleScanScreen
import com.sychip.fhc.app.drawer.screen.PullListScreen
import com.sychip.fhc.app.todo.nav.tab.Screen
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch
import timber.log.Timber


// Keys for navigation
const val ADD_EDIT_RESULT_OK = Activity.RESULT_FIRST_USER + 1
const val DELETE_RESULT_OK = Activity.RESULT_FIRST_USER + 2
const val EDIT_RESULT_OK = Activity.RESULT_FIRST_USER + 3
//
//private object DrawerScreens {
//    const val TASKS_SCREEN = "tasks"
//    const val STATISTICS_SCREEN = "statistics"
//    const val TASKS_DETAIL_SCREEN = "taskDetail"
//    const val Drawer_SCREEN_Dialog = "Dialog"
//    const val Drawer_SCREEN_PullList = "PullList"
//    const val Drawer_SCREEN_Setting = "Setting"
//    const val Drawer_SCREEN_Components = "Components"
//    const val Drawer_SCREEN_Canvas = "Canvas"
//
//    const val Drawer_SCREEN_BLE_Scan = "BLE Scan"
//
//}

object DrawerDestArgs {
    const val USER_MESSAGE_ARG = "userMessage"
    const val TASK_ID_ARG = "taskId"
}
enum class DrawerScreen {
    TASKS,
    STATISTICS,
    TASKS_DETAIL,
    Dialog ,
    PullList,
    Setting,
     Components,
    Canvas,
   BLEScan
}
//object DrawerDestinations {
//    const val TASKS_ROUTE = DrawerScreens.TASKS_SCREEN
//    const val STATISTICS_ROUTE = DrawerScreens.STATISTICS_SCREEN
//    const val Drawer_ROUTE_Dialog = DrawerScreens.Drawer_SCREEN_Dialog
//    const val Drawer_ROUTE_PullList = DrawerScreens.Drawer_SCREEN_PullList
//    const val Drawer_ROUTE_Setting = DrawerScreens.Drawer_SCREEN_Setting
//    const val Drawer_ROUTE_Components = DrawerScreens.Drawer_SCREEN_Components
//    const val Drawer_ROUTE_Canvas = DrawerScreens.Drawer_SCREEN_Canvas
//    const val Drawer_ROUTE_BLE_Scan = DrawerScreens.Drawer_SCREEN_BLE_Scan
//    const
//}

val TASKS_DETAIL_ROUTE_RULE = "${DrawerScreen.TASKS_DETAIL.name}/{${TodoDestinationsArgs.TITLE_ARG}}?${TodoDestinationsArgs.TASK_ID_ARG}={${TodoDestinationsArgs.TASK_ID_ARG}}"
fun getTaskDetailRoute(title:String?,taskId: String?):String{
    return if(title == null && taskId == null){
        DrawerScreen.TASKS_DETAIL.name
    }else if(title != null && taskId == null){
        "${DrawerScreen.TASKS_DETAIL.name}/$title"
    }else if(title == null && taskId != null){
        "${DrawerScreen.TASKS_DETAIL.name}/?${TodoDestinationsArgs.TASK_ID_ARG}=$taskId"
    }else{
        "${DrawerScreen.TASKS_DETAIL.name}/$title?${TodoDestinationsArgs.TASK_ID_ARG}=$taskId"
    }
}

sealed class DrawerItem(val route: String, val label: String, val icon: ImageVector) {
    data object TASKS : DrawerItem(
        DrawerScreen.TASKS.name,
        "Home",
        Icons.Default.Home
    )
    data object STATISTICS : DrawerItem(
        DrawerScreen.STATISTICS.name,
        "Home",
        Icons.Default.Home
    )
    data object Dialog : DrawerItem(
        DrawerScreen.Dialog.name,
        "Dialog",
        Icons.Default.Person)
    data object PullList : DrawerItem(
        DrawerScreen.PullList.name,
        "PullList",
        Icons.AutoMirrored.Default.List)
    data object Setting : DrawerItem(
        DrawerScreen.Setting.name,
        "Settings",
        Icons.Default.Settings)
    data object Components : DrawerItem(
        DrawerScreen.Components.name,
        "组件",
        Icons.Default.KeyboardCommandKey)
    data object Canvas : DrawerItem(
        DrawerScreen.Canvas.name,
        "Canvas",
        Icons.Default.KeyboardCommandKey)
    data object BLEScan : DrawerItem(
        DrawerScreen.BLEScan.name,
        "Canvas",
        Icons.Default.KeyboardCommandKey)

}

/**
 * Models the navigation actions in the app.
 */
class DrawerNaviActions(private val navController: NavHostController) {
    fun navigateToTasks(userMessage: Int = 0) {
        val navigatesFromDrawer = userMessage == 0
        navController.navigate(
            route =  DrawerItem.TASKS.route.let {
                if (userMessage != 0) "$it?${DrawerDestArgs.USER_MESSAGE_ARG}=$userMessage" else it
            },
            navOptions = navOptions {
                anim {
                    enter = 0
                    popEnter = 0
                    popExit = 0
                    exit = 0
                }
                popUpTo(navController.graph.findStartDestination().id) {
                    inclusive = !navigatesFromDrawer
                    saveState = navigatesFromDrawer
                }
                launchSingleTop = true
                restoreState = navigatesFromDrawer
            }
        )
    }


    fun navigateToTaskDetail(title: String, taskId: String) {
        navController.navigate(
            route =  getTaskDetailRoute(title, taskId),
            navOptions = navOptions {
                anim {
                    enter = 0
                    popEnter = 0
                    popExit = 0
                    exit = 0
                }
            }
        )
    }


    fun navigateDrawerToRoute(route: String) {
        navController.navigate(
            route = route,
            navOptions = navOptions {
                anim {
                    enter = 0
                    popEnter = 0
                    popExit = 0
                    exit = 0
                }
                popUpTo(navController.graph.findStartDestination().id) {
                    saveState = true
                }
                launchSingleTop = true
                restoreState = true
            })
    }
}




@OptIn(ExperimentalLayoutApi::class)
@Composable
fun DrawerNavHost(viewModel: DrawerViewModel  = hiltViewModel(),
                  nwViewModel: NetworkViewModel = hiltViewModel(),
                  showCover: Boolean = true,
                  onBack: () -> Unit = {}) {
    val coroutineScope = rememberCoroutineScope()
    val navController = rememberNavController()
    val drawerState = rememberDrawerState(initialValue = DrawerValue.Closed)
    val navActions = remember(navController) { DrawerNaviActions(navController) }
    val startRoute = DrawerItem.TASKS.route

    val networkState by nwViewModel.networkState.collectAsStateWithLifecycle()
    if(networkState == ConnectionState.Unavailable) {
        ToastyShow("ConnectionState.Unavailable"){
            Timber.i("ConnectionState   Unavailable")
        }
    }else{
        ToastyShow("ConnectionState.AAAAvailable"){
            Timber.i("ConnectionState   AAAAvailable")
        }
    }
    val snackbarHostState = rememberSnackyHostState()
    val loadingState by viewModel.loadingState.collectAsStateWithLifecycle()
    GlobalLoadingView(loadingState) {
    Scaffold(
        snackbarHost = {
            SnackyHost(
                hostState = snackbarHostState,
                animationSpec = SnackyAnimationSpec(scaleFactor = 0.5f),
            )
        },
        modifier = Modifier.fillMaxSize(),
        contentWindowInsets = WindowInsets(0, 0, 0, 0),
    ) { innerPadding ->
        BackHandler(onBack = {
            onBack()
        })
//        val testBooleanFlow = viewModel.testBooleanFlow.collectAsStateWithLifecycle()
        val modifier = Modifier.consumeWindowInsets(WindowInsets.navigationBars).padding(innerPadding)
//        ShowQuestionSnackbar(
//            showState = testBooleanFlow,
//            message = "Find a new device",
//            question = "How to do it???",
//            snackbarHostState = snackbarHostState,
//            questionAction = {
//                Timber.i("   questionAction")
////                tabViewModel.testBoolean()
//            },
//            okAction = {
//                Timber.i("   okAction")
//                viewModel.testBoolean()
//            },
//            onDismiss = {
//                Timber.i("   onDismiss")
//                viewModel.testBoolean()
//            }
//        )

        AppModalDrawer(
            drawerState = drawerState,
            currentRoute = startRoute,
            navigationActions = navActions,
            modifier = modifier
        ) {
            NavHost(
                navController, startDestination = startRoute,
                modifier = modifier,

                enterTransition = { EnterTransition.None },  //不允许动画，避免tab切换闪烁
                exitTransition = { ExitTransition.None },
                popEnterTransition = { EnterTransition.None },
                popExitTransition = { ExitTransition.None },
            ) {
                mainDrawerComposable(
                    navController, drawerState, navActions,
                    openDrawer = { coroutineScope.launch { drawerState.open() } },
                    onAddTask = {
                        navActions.navigateToTaskDetail(title = "aaa", taskId = "11")
                    },
                    onTaskClick = { it ->
                        navActions.navigateToTaskDetail(it.title, it.id)
                    }
                )

                statisticsComposable(modifier = modifier,
                    navController, drawerState, navActions,
                    openDrawer = { coroutineScope.launch { drawerState.open() } }
                )

                drawerComposable(modifier = modifier,route = DrawerItem.Dialog.route,
                    drawerState, navActions,
                    openDrawer = { coroutineScope.launch { drawerState.open() } }
                )

                drawerComposable(modifier = modifier,route = DrawerItem.PullList.route,
                     drawerState, navActions,
                    openDrawer = { coroutineScope.launch { drawerState.open() } }
                )
                drawerComposable(modifier = modifier,route = DrawerItem.Setting.route,
                    drawerState, navActions,
                    openDrawer = { coroutineScope.launch { drawerState.open() } }
                )
                drawerComposable(modifier = modifier,route = DrawerItem.Components.route,
                     drawerState, navActions,
                    openDrawer = { coroutineScope.launch { drawerState.open() } }
                )
                drawerComposable(modifier = modifier,route =DrawerItem.Canvas.route,
                     drawerState, navActions,
                    openDrawer = { coroutineScope.launch { drawerState.open() } }
                )
                drawerComposable(modifier = modifier,route = DrawerItem.BLEScan.route,
                    drawerState, navActions,
                    openDrawer = { coroutineScope.launch { drawerState.open() } }
                )


                taskDetailCompose(navController, navActions)
            }
            var showSplash by remember { mutableStateOf(true) }
            LaunchedEffect(null) {
                delay(3500)
                showSplash = false
            }
            if(showCover && showSplash) {
                AppCoverScreen()
            }
        }
    }
}
}



@Composable
 fun AppModalDrawer(
    modifier: Modifier = Modifier,
    drawerState: DrawerState,
    currentRoute: String,
    navigationActions: DrawerNaviActions,
    coroutineScope: CoroutineScope = rememberCoroutineScope(),
    content: @Composable (modifier: Modifier) -> Unit
) {

//    // 设置系统栏颜色 (需 accompanist-systemuicontroller)
//    val systemUiController = rememberSystemUiController()
//    SideEffect {
//        systemUiController.setStatusBarColor(Color.Transparent, darkIcons = false)
//        systemUiController.setNavigationBarColor(Color.Blue)  // 同步底部颜色:ml-citation{ref="5,8" data="citationList"}
//    }
    //不知为何navigate到本画面，AppDrawer会一闪而消失（默认drawerState尽管是close）
    //如果设置requiredWidth从0到280，ModalDrawerSheet就会打开（默认drawerState尽管是close）
    //设置alpha从0-1可以解决
    val alphaDrawer = remember { mutableFloatStateOf(0f) }
    ModalNavigationDrawer (
        modifier = modifier,
        drawerState = drawerState,
        drawerContent = {
            // 处理 Back 事件
            BackHandler(drawerState.isOpen) {
                Timber.i("BackHandler   >>>>>>onBack  drawerState：%s", drawerState.isOpen)
                coroutineScope.launch { drawerState.close() }
            }
            // 抽屉内容
            AppLeftDrawer(
                alphaDrawer = alphaDrawer,
                currentRoute = currentRoute,
                closeDrawer = { coroutineScope.launch { drawerState.close() } },
                navigationActions = navigationActions,
            )
        },
        // 仅在抽屉打开时允许手势开关抽屉，防止手势冲突
        gesturesEnabled = drawerState.isOpen

    ) {

        content(modifier)
    }
    LaunchedEffect(true) {
        delay(100)
        alphaDrawer.floatValue = 1F
    }
}


@Composable
private fun DrawerHeader(
    modifier: Modifier = Modifier
) {
    Column(
        horizontalAlignment = Alignment.CenterHorizontally,
        verticalArrangement = Arrangement.Center,
        modifier = modifier
            .fillMaxWidth()
            .background(colorScheme.primary)
            .height(dimensionResource(id = R.dimen.header_height))
            .padding(dimensionResource(id = R.dimen.header_padding))
    ) {
        Image(
            painter = painterResource(id = R.drawable.logo_no_fill),
            contentDescription = stringResource(id = R.string.tasks_header_image_content_description),
            modifier = Modifier.width(dimensionResource(id = R.dimen.header_image_width))
        )
        Text(
            text = stringResource(id = R.string.navigation_view_header_title),
            color = colorScheme.surface
        )
    }
}

@Composable
private fun DrawerButton(
    painter: Painter,
    label: String,
    isSelected: Boolean,
    action: () -> Unit,
    modifier: Modifier = Modifier
) {
    val tintColor = if (isSelected) {
        colorScheme.secondary
    } else {
        colorScheme.onSurface.copy(alpha = 0.6f)
    }

    TextButton(
        onClick = action,
        modifier = modifier.fillMaxWidth().padding(horizontal = dimensionResource(id = R.dimen.horizontal_margin))
    ) {
        Row(
            horizontalArrangement = Arrangement.Start,
            verticalAlignment = Alignment.CenterVertically,
            modifier = Modifier.fillMaxWidth()
        ) {
            Icon(
                painter = painter,
                contentDescription = null, // decorative
                tint = tintColor
            )
            Spacer(Modifier.width(16.dp))
            Text(
                text = label,
                style = MaterialTheme.typography.bodyMedium,
                color = tintColor
            )
        }
    }
}

@Composable
private fun AppLeftDrawer(
    modifier: Modifier = Modifier,
    navigationActions: DrawerNaviActions,
    alphaDrawer: MutableFloatState,
    currentRoute: String,
    closeDrawer: () -> Unit,
    coroutineScope: CoroutineScope = rememberCoroutineScope()
) {

    ModalDrawerSheet(
        modifier = Modifier.requiredWidth(280.dp).alpha(alphaDrawer.floatValue),   // requiredWidth 重置抽屉宽度为指定值，但是不能过大
        drawerContainerColor = colorScheme.primary,   //status bar 影响背景色
        drawerShape = RectangleShape   //status bar 影响形状
    ) {
        Column(modifier = modifier.fillMaxSize()) {
            DrawerHeader()
            Column(
                modifier = modifier.fillMaxSize().background(Color.White)
            ) {
                DrawerButton(
                    painter = painterResource(id = R.drawable.ic_list),
                    label = stringResource(id = R.string.list_title),
                    isSelected = currentRoute == DrawerItem.TASKS.route,
                    action = {
                        //要先关后navigate，否则Drawer关不好
                        closeDrawer()
                        coroutineScope.launch {
                            delay(250)
                            navigationActions.navigateToTasks()
                        }
                    }
                )
                DrawerButton(
                    painter = painterResource(id = R.drawable.ic_statistics),
                    label = "Drawer_ROUTE_Profile",
                    isSelected = currentRoute ==  DrawerItem.STATISTICS.route,
                    action = {
                        //要先关后navigate，否则Drawer关不好
                        closeDrawer()
                        coroutineScope.launch {
                            delay(250)
                            navigationActions.navigateDrawerToRoute( DrawerItem.STATISTICS.route)
                        }
                    }
                )
                DrawerButton(
                    painter = painterResource(id = R.drawable.ic_statistics),
                    label = "Drawer_ROUTE_Dialog",
                    isSelected = currentRoute ==  DrawerItem.Dialog.route,
                    action = {
                        //要先关后navigate，否则Drawer关不好
                        closeDrawer()
                        coroutineScope.launch {
                            delay(250)
                            navigationActions.navigateDrawerToRoute(DrawerItem.Dialog.route)
                        }
                    }
                )
                DrawerButton(
                    painter = painterResource(id = R.drawable.ic_statistics),
                    label = "Drawer_ROUTE_PullList",
                    isSelected = currentRoute == DrawerItem.PullList.route,
                    action = {
                        closeDrawer()
                        coroutineScope.launch {
                            delay(250)
                            navigationActions.navigateDrawerToRoute( DrawerItem.PullList.route)
                        }
                    }
                )
                DrawerButton(
                    painter = painterResource(id = R.drawable.ic_statistics),
                    label = "Drawer_ROUTE_Setting",
                    isSelected = currentRoute ==  DrawerItem.Setting.route,
                    action = {
                        //要先关后navigate，否则Drawer关不好
                        closeDrawer()
                        coroutineScope.launch {
                            delay(250)
                            navigationActions.navigateDrawerToRoute( DrawerItem.Setting.route)
                        }
                    }
                )
                DrawerButton(
                    painter = painterResource(id = R.drawable.ic_statistics),
                    label = "Drawer_ROUTE_Components",
                    isSelected = currentRoute == DrawerItem.Components.route,
                    action = {
                        //要先关后navigate，否则Drawer关不好
                        closeDrawer()
                        coroutineScope.launch {
                            delay(250)
                            navigationActions.navigateDrawerToRoute(DrawerItem.Components.route)
                        }
                    }
                )
                DrawerButton(
                    painter = painterResource(id = R.drawable.ic_statistics),
                    label = "Canvas",
                    isSelected = currentRoute ==DrawerItem.Canvas.route,
                    action = {
                        //要先关后navigate，否则Drawer关不好
                        closeDrawer()
                        coroutineScope.launch {
                            delay(250)
                            navigationActions.navigateDrawerToRoute(DrawerItem.Canvas.route)
                        }
                    }
                )
                DrawerButton(
                    painter = painterResource(id = R.drawable.ic_statistics),
                    label = "BLE Scan",
                    isSelected = currentRoute == DrawerItem.BLEScan.route,
                    action = {
                        //要先关后navigate，否则Drawer关不好
                        closeDrawer()
                        coroutineScope.launch {
                            delay(250)
                            navigationActions.navigateDrawerToRoute(DrawerItem.BLEScan.route)
                        }
                    }
                )
            }
        }
    }
}

fun NavGraphBuilder.mainDrawerComposable(
    navController: NavHostController,
    drawerState: DrawerState,

    navActions: DrawerNaviActions,
    onAddTask: () -> Unit,
    onTaskClick: (TaskDto) -> Unit,
    openDrawer: () -> Unit,
){

    composable(
        DrawerItem.TASKS.route,
        arguments = listOf(
            navArgument(DrawerDestArgs.USER_MESSAGE_ARG) { type = NavType.IntType; defaultValue = 0 }
        )
    ) { entry ->
        AppModalDrawer(drawerState = drawerState,  currentRoute =DrawerItem.TASKS.route, navigationActions = navActions) {
            Surface {
                Column{
                    TasksScreen(
                        userMessage = entry.arguments?.getInt(DrawerDestArgs.USER_MESSAGE_ARG)!!,
                        onUserMessageDisplayed = { entry.arguments?.putInt(DrawerDestArgs.USER_MESSAGE_ARG, 0) },
                        onAddTask = onAddTask,
                        onTaskClick = onTaskClick,
                        openDrawer = openDrawer,
                        onBack = {  }
                    )
                }
            }
        }

    }
}


fun NavGraphBuilder.statisticsComposable(
    modifier : Modifier = Modifier,
    navController: NavHostController,
    drawerState: DrawerState,
    navActions: DrawerNaviActions,
    openDrawer: () -> Unit,
){
    composable(
        DrawerItem.STATISTICS.route
    ) { entry ->
        AppModalDrawer(modifier = modifier, drawerState = drawerState, currentRoute = DrawerItem.STATISTICS.route, navigationActions = navActions) {
            Surface {
                Column {
                    StatisticsScreen(
                        openDrawer = openDrawer,
                    )
                }
            }
        }
    }
}


fun NavGraphBuilder.drawerComposable(
    modifier : Modifier = Modifier,
    route: String,
    drawerState: DrawerState,
    navActions: DrawerNaviActions,
    openDrawer: () -> Unit,
){
    composable(
        route
    ) { entry ->
        AppModalDrawer(modifier = modifier, drawerState = drawerState, currentRoute = route, navigationActions = navActions) {
            Surface {
                Column {
                    if(route == DrawerItem.STATISTICS.route){
                        StatisticsScreen(
                            openDrawer = openDrawer,
                        )
                    }else if(route == DrawerItem.Dialog.route){
                        DialogSampleScreen(
                            onNavigationClick = openDrawer,
                        )
                    }else if(route == DrawerItem.PullList.route){
                        PullListScreen(
                            onNavigationClick = openDrawer
                        )
                    }else if(route == DrawerItem.Setting.route){
                        SettingsScreen(
                            onNavigationClick = openDrawer,
                        )
                    }else if(route == DrawerItem.Components.route){
                        ComponentsScreen(
                            onNavigationClick = openDrawer,
                        )
                    }else if(route == DrawerItem.Canvas.route){
                        CanvasScreen(
                            onNavigationClick = openDrawer,
                        )
                    }else if(route == DrawerItem.BLEScan.route){
                        BleScanScreen(
                            onNavigationClick = openDrawer,
                        )
                    }
                }
            }
        }
    }
}

  fun NavGraphBuilder.taskDetailCompose(
    navController: NavHostController,
    navActions: DrawerNaviActions,
){

    composable(
        TASKS_DETAIL_ROUTE_RULE,
        arguments = listOf(
            navArgument(TodoDestinationsArgs.TITLE_ARG) { type = NavType.StringType; nullable = false },
            navArgument(TodoDestinationsArgs.TASK_ID_ARG) { type = NavType.StringType; nullable = false },  // integer does not allow nullable values
        )
    ) { entry ->
        val taskId = entry.arguments?.getString(TodoDestinationsArgs.TASK_ID_ARG)
        val title = entry.arguments?.getString(TodoDestinationsArgs.TITLE_ARG)
        AddEditTaskScreen(
            topBarTitle = entry.arguments?.getString(TodoDestinationsArgs.TITLE_ARG)!!,
            onTaskUpdate = {
                navActions.navigateToTasks(
                    if (taskId == null) ADD_EDIT_RESULT_OK else EDIT_RESULT_OK
                )
            },
            onBack = { navController.popBackStack() }
        )
    }
}


